package com.itcat.util;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itcat.mapper.UmsAdminRoleMapper;
import com.itcat.mysql.pojo.UmsRole;
import com.itcat.mysql.vo.RoleCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Cr Mr]
 * @version : [v1.0]
 * @date : [2022/4/3 14:24]
 */
@Service
public class AsyncTimeApi {

    @Autowired
    private UmsAdminRoleMapper mapper;

    @Autowired
    private UmsAdminRoleMapper umsAdminRoleMapper;

    @Async
    public void setRoleAdminCount() {
        List<UmsRole> list1 = umsAdminRoleMapper.selectList(new LambdaQueryWrapper<UmsRole>());
        if (CollUtil.isEmpty(list1)) {
            return;
        }
        List<RoleCount> list = mapper.selectRoleAdminCount();
        for (UmsRole umsRole : list1) {
            boolean flag = false;
            for (RoleCount roleCount : list) {
                if ((flag = umsRole.getId().equals(roleCount.getRoleId()))) {
                    break;
                }
            }
            if (!flag) {
                RoleCount roleCount = new RoleCount();
                roleCount.setRoleId(umsRole.getId());
                roleCount.setCount(0L);
                list.add(roleCount);
            }
        }
        umsAdminRoleMapper.setRoleAdminCount(list);
    }
}
